package com.ibo.ndemo;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ShortCutActivity extends AppCompatActivity {

    private ListView mListView;
    private ShortCutAdapter mAdapter;
    private ShortcutManager mShortcutManager;

    private final int SHORTCUT_COUNT = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shortcut);

        mListView = (ListView) findViewById(R.id.list);
        setupAdapter();
        mListView.setAdapter(mAdapter);

        setupShortcuts();

        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                mAdapter.remove(i);
                removeItem(i);
                return true;
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                itemClick(i);
            }
        });
    }

    private void itemClick(final int i) {
        final EditText inputServer = new EditText(this);
        final User user = mAdapter.getItem(i);
        inputServer.setText(user.getName());
        inputServer.setFocusable(true);

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("输入新的名字:")
        .setIcon(R.mipmap.user_1)
        .setView(inputServer).setNegativeButton("取消", null);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String inputName = inputServer.getText().toString();
                        user.setName(inputName);
                        mAdapter.set(user, i);
                        updItem(i);
                    }
                });
        builder.show();
    }

    private void updItem(int index) {
        Intent intent = new Intent(this, ShortCutMessageActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        User user = mAdapter.getItem(index);
        intent.putExtra("name",  user.getName());
        intent.putExtra("pick", user.getPick());

        ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + index)
                .setShortLabel(user.getName())
                .setLongLabel("联系人:" + user.getName())
                .setIcon(Icon.createWithResource(this, user.getPick()))
                .setIntent(intent)
                .build();

        mShortcutManager.updateShortcuts(Collections.singletonList(info));
    }

    private void removeItem(int index) {
        List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();
        for (ShortcutInfo info : infos) {
            if (info.getId().equals("id" + index)) {
                mShortcutManager.disableShortcuts(Collections.singletonList(info.getId()), "暂无该联系人");
            }
        }
        mShortcutManager.removeDynamicShortcuts(Collections.singletonList("id" + index));
    }

    private void setupShortcuts() {
        mShortcutManager = getSystemService(ShortcutManager.class);

        List<ShortcutInfo> infos = new ArrayList<>();
        for (int i = 0; i < SHORTCUT_COUNT; i++) {
            Intent intent = new Intent(this, ShortCutMessageActivity.class);
            intent.setAction(Intent.ACTION_VIEW);
            User user = mAdapter.getItem(i);
            intent.putExtra("name",  user.getName());
            intent.putExtra("pick", user.getPick());

            ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)
                    .setShortLabel(user.getName())
                    .setLongLabel("联系人:" + user.getName())
                    .setIcon(Icon.createWithResource(this, user.getPick()))
                    .setIntent(intent)
                    .build();
            infos.add(info);
        }

        mShortcutManager.setDynamicShortcuts(infos);
    }

    private void setupAdapter() {
        List<User> list = new ArrayList<>();

        list.add(new User("ibo",R.mipmap.user_4));
        list.add(new User("mother",R.mipmap.user_2));
        list.add(new User("father",R.mipmap.user_3));
        mAdapter = new ShortCutAdapter();
        mAdapter.setDatas(list);
    }
}
